from flask import Flask
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from flask_sqlalchemy import SQLAlchemy

from app.configs import config

db = SQLAlchemy()
jwt = JWTManager()


def create_app(config_name):
    app = Flask(__name__, template_folder="templates")
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    db.init_app(app)
    jwt.init_app(app)

    register_blueprints(app)

    CORS(app, supports_credentials=True)

    return app


def register_blueprints(app):
    from app.api.unorganized import unorganized
    from app.api.tokens import tokens
    from app.api.readers import readers
    from app.api.books import books
    from app.api.isbns import isbns
    app.register_blueprint(unorganized)
    app.register_blueprint(tokens)
    app.register_blueprint(readers)
    app.register_blueprint(books)
    app.register_blueprint(isbns)
